package zelix.utils.system;

import io.netty.channel.*;
import net.minecraft.network.Packet;
import net.minecraft.network.play.client.CPacketPlayer;
import zelix.EventsHandler;
import zelix.eventapi.EventManager;
import zelix.eventapi.event.EventPlayerPre;
import zelix.utils.ReflectionHelper;
import zelix.utils.Wrapper;
import zelix.utils.hooks.visual.ChatUtils;

public class Connection extends ChannelDuplexHandler {

	public static enum Side { IN, OUT; }
    private EventsHandler eventHandler;

    public Connection(EventsHandler eventHandler) {
        this.eventHandler = eventHandler;
        try {
            ChannelPipeline pipeline = Wrapper.INSTANCE.mc().getConnection().getNetworkManager().channel().pipeline();
            pipeline.addBefore("packet_handler", "PacketHandler", (ChannelHandler) this);
        } catch (Exception exception) {
        	ChatUtils.error("Connection: Error on attaching");
            //exception.printStackTrace();
        }
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object packet) throws Exception {

        if ((Packet)packet instanceof CPacketPlayer.PositionRotation){
            CPacketPlayer.PositionRotation packetPlayer = (CPacketPlayer.PositionRotation)packet;
            //processPlayerPacket(packetPlayer);
            double x =  ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"x","field_149479_a");
            double y = ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"y","field_149477_b");
            double z = ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"z","field_149478_c");
            float yaw =  ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"yaw","field_149476_e");
            float pitch = ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"pitch","field_149473_f");
            boolean onGroud = ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"onGround","field_149474_g");
            EventPlayerPre eventPlayerPre = new EventPlayerPre(x,y,z,yaw,pitch,Wrapper.INSTANCE.player().isSneaking(),onGroud);;
            EventManager.call(eventPlayerPre);
            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getX(),"x","field_149479_a");
            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getY(),"y","field_149477_b");
            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getZ(),"z","field_149478_c");
            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getYaw(),"yaw","field_149476_e");
            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getPitch(),"pitch","field_149473_f");
        }


        if (!eventHandler.onPacket(packet, Side.IN)) return;
        super.channelRead(ctx, packet);
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object packet, ChannelPromise promise) throws Exception {

        if ((Packet)packet instanceof CPacketPlayer.PositionRotation){
            CPacketPlayer.PositionRotation packetPlayer = (CPacketPlayer.PositionRotation)packet;
            //processPlayerPacket(packetPlayer);
            double x =  ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"x","field_149479_a");
            double y = ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"y","field_149477_b");
            double z = ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"z","field_149478_c");
            float yaw =  ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"yaw","field_149476_e");
            float pitch = ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"pitch","field_149473_f");
            boolean onGroud = ReflectionHelper.getPrivateValue(CPacketPlayer.class,packetPlayer,"onGround","field_149474_g");
            EventPlayerPre eventPlayerPre = new EventPlayerPre(x,y,z,yaw,pitch,Wrapper.INSTANCE.player().isSneaking(),onGroud);

            EventManager.call(eventPlayerPre);

            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getX(),"x","field_149479_a");
            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getY(),"y","field_149477_b");
            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getZ(),"z","field_149478_c");
            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getYaw(),"yaw","field_149476_e");
            ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.getPitch(),"pitch","field_149473_f");

           // if (!eventHandler.onPacket(packetPlayer, Side.OUT))
        }


        if (!eventHandler.onPacket(packet, Side.OUT)) return;
        super.write(ctx, packet, promise);
    }

    public void processPlayerPacket(CPacketPlayer.PositionRotation packetPlayer){


        //ReflectionHelper.setPrivateValue(CPacketPlayer.class,packetPlayer,eventPlayerPre.get(),"onGround","field_149474_g");



    }}
